package com.r2coding.log.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.r2coding.log.dao.LogDao;
import com.r2coding.log.entity.LogEntity;
import com.r2coding.log.service.LogService;
import com.r2coding.log.vo.QueryLogReqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日志信息
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-05-26 22:10:59
 */
@Slf4j
@Service("logService")
public class LogServiceImpl extends ServiceImpl<LogDao, LogEntity> implements LogService {
    @RabbitListener(queues = {"IBlog_Log"})
    @Override
    public void receiveMessage(String message) {
        log.info("记录日志: {}", message);
        JSONObject jsonObject = JSONObject.parseObject(message);
        Integer userId = (Integer) jsonObject.get("userId");
        String userName = (String) jsonObject.get("userName");
        String result = (String) jsonObject.get("result");
        String description = (String) jsonObject.get("description");
        String time = (String) jsonObject.get("time");
        String module = (String) jsonObject.get("module");
        String operationType = (String) jsonObject.get("operationType");
        String ipAddr = (String) jsonObject.get("ipAddr");
        String createTime = (String) jsonObject.get("createTime");
        LogEntity log = new LogEntity();
        log.setUserId(userId)
           .setUserName(userName)
           .setResult(result)
           .setDescription(description)
           .setTime(time)
           .setModule(module)
           .setOperationType(operationType)
           .setIpAddr(ipAddr)
           .setCreateTime(createTime);
        this.baseMapper.insert(log);
    }

    @Override
    public PageInfo<LogEntity> queryList(QueryLogReqVo vo) {
        QueryWrapper<LogEntity> wrapper = new QueryWrapper<>();
        PageHelper.startPage(vo.getCurrent(), vo.getSize());
        if (!StringUtils.isEmpty(vo.getOperationType())) {
            wrapper.eq("operation_type", vo.getOperationType());
        }
        if (!StringUtils.isEmpty(vo.getResult())) {
            wrapper.eq("result", vo.getResult());
        }
        if (!StringUtils.isEmpty(vo.getModule())) {
           wrapper.eq("module", vo.getModule());
        }
        if (!StringUtils.isEmpty(vo.getUserName())) {
            wrapper.like("user_name", vo.getUserName());
        }
        if (!StringUtils.isEmpty(vo.getStartTime())) {
            wrapper.ge("create_time", vo.getStartTime());
        }
        if (!StringUtils.isEmpty(vo.getEndTime())) {
            wrapper.le("create_time", vo.getEndTime());
        }
        wrapper.orderByDesc("create_time");
        List<LogEntity> list = this.baseMapper.selectList(wrapper);
        return new PageInfo<LogEntity>(list);
    }
}